<script setup lang="ts">
import { ref, watch } from 'vue'
import { storageLocal } from '@pureadmin/utils'

import type { SortRule, ISettings } from './types'
import type { SelectOption } from 'naive-ui'

interface IRuleOption extends SelectOption {
  label: string
  value: SortRule
}

interface ITechniquesOption extends SelectOption {
  label: string
  value: number
}

const sortRuleOptions: IRuleOption[] = [
  { label: '金币最多', value: 'MAX_REVENUE' },
  { label: '时间最少', value: 'MIN_TIME' },
]

const techniquesOptions: ITechniquesOption[] = [
  { label: '可口', value: 1 },
  { label: '优级', value: 2 },
  { label: '特级', value: 3 },
  { label: '神级', value: 4 },
  { label: '传奇', value: 5 },
]

const localSettings = storageLocal().getItem<ISettings | null>('RestaurantOption')

const settings = ref<ISettings>({
  sortRule: localSettings?.sortRule ?? sortRuleOptions[0].value,
  threads: localSettings?.threads ?? navigator.hardwareConcurrency,
  operatingTime: localSettings?.operatingTime ?? 12,
  maxChefCount: localSettings?.maxChefCount ?? 3,
  maxRecipeCount: localSettings?.maxRecipeCount ?? 3,
  techniques: localSettings?.techniques ?? techniquesOptions[0].value,
})

watch(settings.value, (value) => {
  storageLocal().setItem('RestaurantOption', value)
})

function getSettings(): ISettings {
  return {
    ...settings.value,
    operatingTime: settings.value.operatingTime * 3600,
  }
}

defineExpose({ getSettings })
</script>

<template>
  <n-flex>
    <n-select v-model:value="settings.sortRule" :options="sortRuleOptions" style="width: 120px" />
    <n-input-number v-model:value="settings.threads" :min="1" :max="128" style="width: 120px">
      <template #prefix>线程</template>
    </n-input-number>
    <n-input-number v-model:value="settings.operatingTime" :min="1" :max="48" style="width: 120px">
      <template #prefix>营业</template>
    </n-input-number>
    <n-input-number v-model:value="settings.maxChefCount" :min="1" :max="3" style="width: 120px">
      <template #prefix>厨师</template>
    </n-input-number>
    <n-input-number v-model:value="settings.maxRecipeCount" :min="1" :max="3" style="width: 120px">
      <template #prefix>菜谱</template>
    </n-input-number>
    <n-select
      v-model:value="settings.techniques"
      :options="techniquesOptions"
      style="width: 120px"
    />
  </n-flex>
</template>
